package com.smiier.skin;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.pifuke.adapter.TreatmentProjectListForHospitalAdapter;
import com.pifuke.adapter.UserAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.extra.TreatmentProjectItemExtra;
import com.smiier.skin.extra.UserExtra;
import com.smiier.skin.net.DoctorTreatmentProjectListTask.DoctorTreatmentProjectItem;
import com.smiier.skin.net.HospitalHomeDataTask;
import com.smiier.skin.net.HospitalHomeDataTask.HospitalHomeDataRequest;
import com.smiier.skin.net.HospitalHomeDataTask.HospitalHomeDataResponse;
import com.smiier.skin.net.HospitalHomeDataTask.TreatmentItem;
import com.smiier.skin.net.entity.Hospital.HospitalItem;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.util.ShareUtil;
import com.umeng.analytics.MobclickAgent;

public class HospitalActivity extends BasicActivity {
	User mUser;
	TextView mName;
	TextView mLocation;
	TextView mDescrption;
	HospitalItem mItem;
	ListView mDoctor;
	ListView mTreatment;
	UserAdapter mDoctorAdapter;
	LinearLayout mDoctorParent;
	LinearLayout mTreatmentParent;

	TreatmentProjectListForHospitalAdapter mTreatmentAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hospital_detail);
		init();
		Intent i = getIntent();
		UserExtra extra = new UserExtra();
		extra.getFrom(i);
		mUser = extra.getUser();
		mName = findViewById(R.id.hospital_name, TextView.class);
		mLocation = findViewById(R.id.hospital_loc, TextView.class);
		mDescrption = findViewById(R.id.desc_content, TextView.class);
		mDoctor = findViewById(R.id.doctor_list, ListView.class);
		mTreatment = findViewById(R.id.treatments_list, ListView.class);
		mTreatment.setOnItemClickListener(new TreatmentOnItemClick());
		mDoctor.setOnItemClickListener(new DoctorOnItemClick());
		mDoctorParent = findViewById(R.id.doctor_parent, LinearLayout.class);
		mTreatmentParent = findViewById(R.id.treatments_parent,
				LinearLayout.class);
		mDoctorAdapter = new UserAdapter();
		mDoctorAdapter.setActivity(this);
		mTreatmentAdapter = new TreatmentProjectListForHospitalAdapter();
		mTreatment.setAdapter(mTreatmentAdapter);
		mDoctor.setAdapter(mDoctorAdapter);
		String hos = mUser.Hospital;
		boolean has = false;
		for (HospitalItem item : GlobalSettings.mHospitalItems) {
			if (hos != null && !hos.equals("")) {
				if (item.Name.equals(hos) || item.FullName.equals(hos)
						|| item.Name.contains(hos)
						|| item.FullName.contains(hos)
						|| hos.contains(item.Name)
						|| hos.contains(item.FullName)) {
					if (item.Level != null && !item.Level.equals("")) {
						mItem = item;
						has = true;
						mName.setText(item.FullName);
						break;
					}
				}
			}
		}
		if (has) {
			setNavTitle(mItem.Name);
			mLocation.setText(mItem.Address);
			mDescrption.setText(mItem.Desc);
			setNavRightBtnIcon(R.drawable.btn_share);
		} else {
			finish();
		}
		HospitalHomeDataTask task = new HospitalHomeDataTask();
		HospitalHomeDataRequest req = new HospitalHomeDataRequest();
		req.hospital = mItem.FullName;
		task.setRequest(req);
		task.addListener(new NetTaskListener<HospitalHomeDataTask.HospitalHomeDataRequest, HospitalHomeDataTask.HospitalHomeDataResponse>() {

			@Override
			public void onException(
					INetTask<HospitalHomeDataRequest, HospitalHomeDataResponse> task,
					Exception e) {

			}

			@Override
			public void onComplete(
					INetTask<HospitalHomeDataRequest, HospitalHomeDataResponse> task,
					HospitalHomeDataResponse response) {
				if (response != null) {
					if (response.doctor_info != null
							&& response.doctor_info.size() > 0) {
						mDoctorAdapter.setDataProvider(response.doctor_info);
						mDoctorAdapter.notifyDataSetChanged();
					} else {
						mDoctorParent.setVisibility(View.GONE);
					}

					if (response.treatmentproject != null
							&& response.treatmentproject.size() > 0) {
						mTreatmentAdapter
								.setDataProvider(response.treatmentproject);
						mTreatmentAdapter.notifyDataSetChanged();
					} else {
						mTreatmentParent.setVisibility(View.GONE);
					}
				}

			}
		});
		add(task);
	}

	private class TreatmentOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			TreatmentItem item = mTreatmentAdapter.getDataProvider().get(
					position);
			DoctorTreatmentProjectItem dt = new DoctorTreatmentProjectItem();
			dt.discount_price = item.discount_price;
			dt.hospital = item.hospital;
			dt.id = item.id;
			dt.original_price = item.original_price;
			dt.url = item.url;
			Intent i = new Intent(getContext(), TreatmentDetailActivity.class);
			TreatmentProjectItemExtra extra = new TreatmentProjectItemExtra();
			extra.setUser(dt);
			if (extra.putTo(i)) {
				startActivity(i);
			}
		}

	}

	private class DoctorOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			User user = mDoctorAdapter.getDataProvider().get(position).User;
			Intent intent = new Intent(getContext(), DoctorDetailActivity.class);
			try {
				intent.putExtra(Constant.IDENTITY_KEY, JsonUtil
						.convertToObject(user).toString());
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.text_right) {
			ShareUtil.share(this, GlobalSettings.SERVER_URL_TREATMENT
					+ "Client/TreatmentProject/hospitalHome/hospital/"
					+ mItem.Name, mItem.FullName, "");
		}
	}
	
	
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/

}
